package com.cskaoyan.service.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.vo.wx.HomeIndexInnerFloorGoodsListVO;
import com.cskaoyan.bean.vo.wx.HomeIndexVO;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cskaoyan.bean.MarketGoods.Column;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author klw
 * @Data 2022/11/22 上午11:05
 */

@Service
public class HomeServiceImpl implements HomeService{
    Column[] columns = new Column[]{Column.id, Column.name, Column.brief, Column.picUrl, Column.isHot, Column.isNew, Column.counterPrice, Column.retailPrice};


    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Autowired
    MarketAdMapper marketAdMapper;

    @Autowired
    MarketSystemMapper marketSystemMapper;


//    List<MarketAd> banner;  //广告
//    List <MarketBrand> brandList; // 品牌
//    List <MarketCategory> channel; //类目
//    List <MarketCoupon> couponList; //优惠卷
//    List <HomeIndexInnerFloorGoodsListVO> floorGoodsList; //类目下的商品显示
//
//    List <MarketGoods> hotGoodsList; // isHot商品
//    List <MarketGoods> newGoodsList; // isNew商品
//    List <MarketTopic> topicList; // 专题

    @Override
    public HomeIndexVO listHomeIndex() {

        //限定查出的条数,与后台的小程序配置对应
        List <MarketSystem> marketSystems = marketSystemMapper.selectConfigs();
        Map<String, String> map = marketSystems.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(MarketSystem::getKeyName, MarketSystem::getKeyValue));

        int market_wx_catlog_goods = Integer.parseInt(map.get("market_wx_catlog_goods")); //类目下的商品
        int market_wx_catlog_list = Integer.parseInt(map.get("market_wx_catlog_list")); //类目(floorGoodslist)
        int market_wx_index_new = Integer.parseInt(map.get("market_wx_index_new")); // new
        int market_wx_index_topic = Integer.parseInt(map.get("market_wx_index_topic")); //topic
        int market_wx_index_brand = Integer.parseInt(map.get("market_wx_index_brand")); // brand
        int market_wx_index_hot = Integer.parseInt(map.get("market_wx_index_hot")); //hot

        //查询各个表
        List<MarketAd> banner = marketAdMapper.selectAll();
        List <MarketBrand> brandList = marketBrandMapper.selectAllBrandsLimit(market_wx_index_brand);
        List <MarketCategory> channel = marketCategoryMapper.selectAll();
        List <MarketCoupon> couponList = marketCouponMapper.selectAllK();


        //分次把类目和商品 限制个数的查出来  -- gg
        List <HomeIndexInnerFloorGoodsListVO> floorGoodsList = marketCategoryMapper.selectFloorGoodsList(market_wx_catlog_list);
        if(null != floorGoodsList){
            for (HomeIndexInnerFloorGoodsListVO homeIndexInnerFloorGoodsListVO : floorGoodsList) {
                int id = homeIndexInnerFloorGoodsListVO.getId();
                List<Integer> childIds = marketCategoryMapper.selectChildIds(id);

                List <MarketGoods> goods;
                if(childIds.size() == 0){
                    goods = new ArrayList<>();
                }else {
                    MarketGoodsExample example = new MarketGoodsExample();
                    example.setDistinct(true);
                    example.setStartRow(0);
                    example.setPageSize(market_wx_catlog_goods);

                    MarketGoodsExample.Criteria criteria = example.createCriteria();
                    criteria.andCategoryIdIn(childIds).andIsOnSaleEqualTo(1).andDeletedEqualTo(0);
                    //PageHelper.startPage(0,market_wx_catlog_goods);
                    goods = marketGoodsMapper.selectByExample(example);
                }
//                    List <MarketGoods> goods = new ArrayList<MarketGoods>();
//                    for (Integer childId : childIds) {
//                         goods.addAll(marketGoodsMapper.selectGoodsByCatId(childId,market_wx_catlog_goods));
//                    }
                homeIndexInnerFloorGoodsListVO.setGoodsList(goods);

            }
        }

        List <MarketGoods> hotGoodsList = marketGoodsMapper.selectHotGoods(market_wx_index_hot);
        List <MarketGoods> newGoodsList = marketGoodsMapper.selectNewGoods(market_wx_index_new);
        List <MarketTopic> topicList = marketTopicMapper.selectAllK(market_wx_index_topic);

        HomeIndexVO homeIndexVO = new HomeIndexVO(banner,brandList,channel,couponList,floorGoodsList,hotGoodsList,newGoodsList,topicList);
        return homeIndexVO;
    }

    @Override
    public Map<String, Object> getAbout() {
        List <MarketSystem> marketSystems = marketSystemMapper.selectConfigs();
        Map<String, String> map = marketSystems.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(MarketSystem::getKeyName, MarketSystem::getKeyValue));

        Map<String, Object> about = new HashMap<>();
        about.put("name", map.get("market_mall_name"));
        about.put("address", map.get("market_mall_address"));
        about.put("phone", map.get("market_mall_phone"));
        about.put("qq", map.get("market_mall_qq"));
        about.put("longitude", map.get("market_mall_longitude"));
        about.put("latitude", map.get("market_mall_latitude"));

        return about;
    }


}
